﻿using System.Collections.Generic;
using System.Net.Mail;

namespace Keyer.Util.mail
{
    public class MailModel
    {
        //主题  
        public string Subject { get; set; }
        public string SubjectEncoding { get; set; }
        //内容  
        public string Body { get; set; }
        public string BodyEncoding { get; set; }
        //附件  
        public List<Attachment> Attachments = new List<Attachment>();
        public MailMessage message;
        //发送人  
        public UserModel from;

        public MailModel(string subject, string body, UserModel f)
        {
            message = new MailMessage();
            this.Subject = subject;
            this.Body = body;
            this.from = f;
        }
        //添加附件  
        public void AddAttach(Attachment file)
        {
            Attachments.Add(file);
        }
        //添加一群附件  
        public void AddAttach(List<Attachment> files)
        {
            foreach (Attachment item in files)
            {
                if (item != null)
                    this.Attachments.Add(item);
            }
        }
        //返回邮件实体  
        public MailMessage GetMail()
        {
            if (this.message != null)
            {
                message.Subject = this.Subject;
                message.SubjectEncoding = System.Text.Encoding.UTF8;
                message.Body = this.Body;
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.From = from.Send_Address;//设置发邮件人地址  
                foreach (Attachment item in Attachments)
                {
                    if (item != null)
                        this.message.Attachments.Add(item);
                }
                return message;
            }
            else
                return null;
        }
    }
}
